/*
 * Copyright 2012 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package jim.h.commons;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;

/**
 * @author jinggang.huangjg 2012-6-14 下午4:53:00
 */
public class TestXml {

    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
        ObjectMapper mapper = new XmlMapper().configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
        MyObject myObject = new MyObject();
        List<Object> list = new ArrayList<Object>();
        list.add("你好");
        list.add("asd #### xx");
        list.add(new MyBean("jim", 10));
        myObject.setList(list);
        String xml = mapper.writeValueAsString(myObject);
        System.out.println(xml);

        myObject = mapper.readValue(xml, MyObject.class);
        System.out.println(myObject);
    }

    @JacksonXmlRootElement(localName = "Root")
    static class MyObject {

        private List<Object> list;

        public List<Object> getList() {
            return list;
        }

        public void setList(List<Object> list) {
            this.list = list;
        }
    }

    static class MyBean {

        private String name;

        private int    age;

        public MyBean(String name, int age){
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
